//
//  CYTransferHeaderView.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/7/7.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYTransferHeaderView.h"
@interface CYTransferHeaderView(){
    transferHeaderBlock _myblock;
}
@property (nonatomic, strong) NSArray *titles;
@end
@implementation CYTransferHeaderView
- (instancetype)initWithTitles:(NSArray *)array block:(transferHeaderBlock)block{
    self = [super init];
    if (self) {
        _myblock = block;
        _titles = array;
        [self initializeSubViews];
        self.currentIndex = 0;
    }
    return self;
}

- (void)setCurrentIndex:(NSUInteger)currentIndex{
    if (_currentIndex != currentIndex)
        _currentIndex = currentIndex;
    
    [self performSelector:@selector(actionForBtn:) withObject:[self viewWithTag:200 + currentIndex]];
    
}
- (void)initializeSubViews{
    
    for (int i = 0; i < _titles.count; i ++) {
        
        CGFloat mid_originX = SCREEN_WIDTH / 6.0 * (2 * i + 1) - SCREEN_WIDTH / 2.0;  //每个label的位置在父视图的横向中间点
        CYButton *button = [[CYButton alloc]initWithTitle:_titles[i] titlesize:13 cornerRadius:0.0];
        [button addTarget:self action:@selector(actionForBtn:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitleColor:TEXT_GRAY_COLOR forState:UIControlStateNormal];
        [button setTitleColor:TEXT_BLACK_COLOR forState:UIControlStateSelected];
        button.tag = 200 + i;
        [self addSubview:button];
        
        CGSize size = [_titles[i] sizeWithAttributes:@{NSFontAttributeName :[UIFont systemFontOfSize:13]}];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self).with.offset(0);
            make.centerX.equalTo(self).with.offset(mid_originX);
            make.size.mas_equalTo(CGSizeMake(AUTO(ceil(size.width)), AUTO(ceil(size.height))));
        }];
        
        if (i < 2) {
            //中间的箭头图片
            CGFloat img_mid_originX = SCREEN_WIDTH / 3.0 * (i + 1)  - SCREEN_WIDTH / 2.0;
            CYImageView *img = [[CYImageView alloc]initWithImageName:@"zuola" size:CGSizeMake(6, 10) isRadius:NO];
            img.tag = 300 + i;
            [self addSubview:img];
            
            [img mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(button);
                make.centerX.equalTo(self).with.offset(img_mid_originX);
                make.size.mas_equalTo(CGSizeMake(6, 10));
            }];
            
            //上下line背景条
            CYView *line = [[CYView alloc]initWithColor:ColorHex(0xf5f5f9)];
            [self addSubview:line];
            
            [line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(self);
                make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 6));
                if (i == 0)
                    make.top.equalTo(self).with.offset(0);
                else
                    make.bottom.equalTo(self).with.offset(0);
            }];
        }
    }
}

- (void)actionForBtn:(UIButton *)sender{
    if (_currentIndex >= sender.tag - 200){
        _currentIndex = (sender.tag - 200 == _currentIndex)?_currentIndex:(sender.tag - 200);
        sender.selected = YES;
        for (CYView *subview in [self subviews]) {
            if ([subview isKindOfClass:[CYButton class]] && subview.tag != sender.tag){
                CYButton *button = (CYButton *)subview;
                button.selected = NO;
            }
        }
        _myblock(sender.tag - 200);
    }
}
//
//- (void)showBlackColor:(NSUInteger)index{
//
//    for (CYView *subview in [self subviews]) {
//        if ([subview isKindOfClass:[CYButton class]] && index < 3){
//            CYButton *button = (CYButton *)subview;
//            button.selected = (button.tag = index + 200)?YES:NO;
//        }
//    }
//
//}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
